<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller\Api;

use App\Controller\AbstractController;
use App\Model\Moment;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;

#[Controller]
class MomentController extends AbstractController
{
    #[GetMapping(path: '/api/moment/list')]
    public function list()
    {
        $pageNum = $this->request->input('pageNum', 1);
        $pageSize = $this->request->input('pageSize', 10);
        $moment = Moment::query()->select('id', 'user_id', 'content', 'images', 'create_time as createTime')->with('user')->forPage($pageNum, $pageSize)->get()->map(function ($item) {
            $item->avatar = $item->user->avatar;
            $item->nickname = $item->user->nickname;
            unset($item->user);
            return $item;
        });
        $data['records'] = $moment;
        return success($data);
    }
}
